About 2051 letters

About 10 minutes

#Python's input & type conversion

#Standard input

We have already learned how to use the print function to display output. So, how do we get user input?

Calling the input function can get input from the command line, and the return value is of type str:

text:str = input("Please input:")

print("What you input is", text)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Type conversion

If we need to input an integer, we need to convert the value to a different type. The syntax for type conversion is type(value):

G:float = 9.8 # Gravity acceleration

text:str = input("Please enter the mass of the object (kg):")
mass:float = float(text)
print("The gravity on the object is", G * mass)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Practise

Please implement the calculation of the surface area and volume of the sphere, and obtain the radius through input.

  • The formula for the surface area of ​​a sphere is
  • The formula for the volume of a sphere is
PI:float = 3.1415926

radius:float = 0        # Modify the code here to get the radius through input
area:float = 0          # Modify the code here to calculate the surface area
volume:float = 0        # Modify the code here to calculate the volume

print("The surface area and volume of ​​a circle with a radius of", radius, "are", area, "and", volume)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Updated: 5/21/2025

Author: PlanC

Created: 5/15/2025